package com.example.order.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.model.dto.Order;
import com.example.model.dto.Product;
import com.example.order.feign.ProductFeignClient;
import com.example.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * @Author: ft
 * @Date:2025/8/21
 * @Description: 相关单据：
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    ProductFeignClient productFeignClient;

    @SentinelResource(value = "createOrder",blockHandler = "handleBlock")
    @Override
    public Order createOrder(String orderId, String productId) {
        Product forObject = productFeignClient.getProduct(productId);

        Order order = new Order();
        order.setOrderId(orderId);
        order.setOrderAmount(null);
        order.setProducts(Arrays.asList(forObject));
        return order;
    }

    public Order handleBlock(String orderId, String productId, BlockException ex) {
        System.out.println("ex = " + ex.getMessage());
        Order order = new Order();
        order.setOrderId("test111111111111");
        order.setOrderAmount(null);
        order.setProducts(Arrays.asList(new Product()));
        return order;
    }
}